package org.example.servlet;

import org.example.dao.ProductDao1;
import org.example.dao.ProductDAOImpl;
import org.example.model.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/product")
public class ProductServlet extends HttpServlet {
    private ProductDao1 productDAO = new ProductDAOImpl();
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String productId = request.getParameter("id");
        
        if (productId != null) {
            Product product = productDAO.findById(Integer.parseInt(productId));
            if (product != null) {
                request.setAttribute("product", product);
                request.getRequestDispatcher("/product.jsp").forward(request, response);
                return;
            }
        }
        
        response.sendRedirect("index.jsp");
    }
} 